package com.example.designpatternproject.proxy;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.base.BaseActivity;
import com.example.designpatternproject.databinding.ActivityProxyBinding;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @ClassName: ProxyActivity
 * @Author: 史大拿
 * @CreateDate: 10/26/22$ 7:39 PM$
 * TODO 代理模式
 */
public class ProxyActivity extends BaseActivity<ActivityProxyBinding> {
    @Override
    public void initView(Bundle savedInstanceState) {

        View.OnClickListener click = (View.OnClickListener) Proxy.newProxyInstance(
                View.OnClickListener.class.getClassLoader(),
                new Class[]{View.OnClickListener.class},
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Toast.makeText(getApplicationContext(), "点击了", Toast.LENGTH_SHORT).show();
                        Log.i("szjInvoke", "method:" + method.getName() + "\targs:" + Arrays.toString(args));
                        return null;
                    }
                }
        );

        binding.bt.setOnClickListener(click);
//        click.onClick(binding.bt);


//        ImplProxy implProxy = new ImplProxy();
//
//        IProxy proxy = (IProxy) Proxy.newProxyInstance(
//                IProxy.class.getClassLoader(),
//                new Class[]{IProxy.class},
//                new InvocationHandler() {
//                    @Override
//                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                        Log.i("szjInvoke", "name:" + method.getName() + "\tages:" + Arrays.toString(args) );
//
//                        return method.invoke(implProxy, args);
//                    }
//                }
//        );
//
//        String test1 = proxy.test1("张三", 5);
//        Log.i("szj返回", test1);

    }
}
